const { readFile, fsExistsSync } = require('../../../app/util');

function fixedSchemaToData(fixedStr) {
  switch (fixedStr) {
    case 'top':
      return 1;
    case 'bottom':
      return 3;
    case 'require':
      return 4;
    default:
      return 2;
  }
}

const defaultPageSetting = [
  {
    name: {
      zh_CN: '首页',
      en_US: 'Home page',
    },
    route: 'index/index',
    max_block: 40,
    route_handle: '',
    layout: true,
    sections: [],
  },
  {
    name: {
      zh_CN: '商品详情页',
      en_US: 'Product details page',
    },
    route: 'product/detail',
    max_block: 40,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'product_detail',
        fixed: '',
        name: {
          zh_CN: '商品详情',
          en_US: 'Product details',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '专辑列表',
      en_US: 'Collection list',
    },
    route: 'collection/list',
    max_block: 3,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'collection_list',
        fixed: 'top',
        name: {
          zh_CN: '专辑列表',
          en_US: 'Collection list',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '专辑详情',
      en_US: 'Collection Details',
    },
    route: 'collection/detail',
    max_block: 20,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'collection_detail',
        fixed: '',
        name: {
          zh_CN: '专辑详情',
          en_US: 'Collection Details',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '活动专题列表',
      en_US: 'Event Topic list',
    },
    route: 'topic/list',
    max_block: 3,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'topic_list',
        fixed: 'top',
        name: {
          zh_CN: '活动专题列表',
          en_US: 'Event Topic list',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '活动专题详情',
      en_US: 'Event Details',
    },
    route: 'topic/detail',
    max_block: 40,
    route_handle: '',
    layout: true,
    sections: [],
  },
  {
    name: {
      zh_CN: '搜索页',
      en_US: 'Search page',
    },
    route: 'search/detail',
    max_block: 12,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'search_detail',
        fixed: 'require',
        name: {
          zh_CN: '搜索页',
          en_US: 'Search page',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '优惠码列表页',
      en_US: 'Coupon list page',
    },
    route: 'coupon/list',
    max_block: 20,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'coupons_list',
        fixed: 'require',
        name: {
          zh_CN: '优惠券列表',
          en_US: 'Coupon list',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '优惠码详情页',
      en_US: 'Coupon detail page',
    },
    route: 'coupon/detail',
    max_block: 20,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'coupons_detail',
        fixed: 'require',
        name: {
          zh_CN: '优惠券详情',
          en_US: 'Coupon details',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '满减活动列表页',
      en_US: 'Promotion list page',
    },
    route: 'promotion/list',
    max_block: 20,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'promotion_list',
        fixed: 'require',
        name: {
          zh_CN: '活动列表',
          en_US: 'Promotion list',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '满减活动详情页',
      en_US: 'Promotion detail page',
    },
    route: 'promotion/detail',
    max_block: 20,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'promotion_detail',
        fixed: 'require',
        name: {
          zh_CN: '活动详情',
          en_US: 'Promotion detail',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '博客列表',
      en_US: 'Blog list',
    },
    route: 'blog/list',
    max_block: 12,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'blog_list',
        fixed: 'require',
        name: {
          zh_CN: '博客列表',
          en_US: 'Blog list',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '博客详情页',
      en_US: 'Blog detail page',
    },
    route: 'blog/detail',
    max_block: 40,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'blog_detail',
        fixed: 'require',
        name: {
          zh_CN: '博客详情页',
          en_US: 'Blog detail page',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '博客专辑列表',
      en_US: 'Blog album list',
    },
    route: 'news/list',
    max_block: 20,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'blog_collection',
        fixed: 'require',
        name: {
          zh_CN: '博客专辑列表',
          en_US: 'Blog album list',
        },
      },
    ],
  },
  {
    name: {
      zh_CN: '博客专辑详情页',
      en_US: 'Blog collection detail',
    },
    route: 'news/detail',
    max_block: 20,
    route_handle: '',
    layout: true,
    sections: [
      {
        type: 'blog_collection_detail',
        fixed: 'require',
        name: {
          zh_CN: '博客专辑详情页',
          en_US: 'Blog collection detail',
        },
      }
    ],
  },
  {
    name: {
      zh_CN: '404页',
      en_US: '404 page',
    },
    route: 'module/notfound',
    max_block: 20,
    route_handle: '',
    layout: true,
    sections: [],
  }
 
];

// 模版页面setting配置转换
function getPagesSetting(template, blocksSetting) {
  const pagesSchema = [];
  const pagesData = [];
  const pageSetting=[...defaultPageSetting]

  if (fsExistsSync(`./src/theme/${template}/lib/config/comments_list.json`) && defaultPageSetting.findIndex((el) => el.route === 'comments/list') === -1) {
    // 如果存在评论列表配置，则添加评论页
    pageSetting.push({
      name: {
        zh_CN: '评论页',
        en_US: 'Comments Page',
      },
      route: 'comments/list',
      max_block: 20,
      route_handle: '',
      layout: true,
      sections: [],
    });
  }

  pageSetting.forEach((page) => {
    let copyPage = JSON.parse(JSON.stringify(page));
    let sections = JSON.parse(readFile(`./src/theme/${template}/lib/config/${page.route.replace('/', '_')}.json`) || '[]');

    sections.forEach((item, index) => {
      if (copyPage.sections.length && copyPage.sections[0].type === item.type) {
        return;
      }
      copyPage.sections.push({
        type: item.type,
        fixed: item.fixed,
        sort: typeof item.sort === 'number' ? item.sort : undefined,
        name: blocksSetting[item.type].name,
      });
    });
    // copyPage.sections sort 字断排序 然后删除sort
    copyPage.sections.sort((a, b) => {
      const aHasSort = typeof a.sort === 'number';
      const bHasSort = typeof b.sort === 'number';
      if (aHasSort && bHasSort) {
        return a.sort - b.sort;
      }
      if (aHasSort) return -1;
      if (bHasSort) return 1;
      return 0;
    });
    copyPage.sections.forEach((item) => {
      delete item.sort;
    });

    if (copyPage.layout) {
      copyPage.sections.unshift({
        type: 'header',
        fixed: 'top',
        name: {
          zh_CN: '页头',
          en_US: 'Page header',
        },
      });
      copyPage.sections.push({
        type: 'footer',
        fixed: 'bottom',
        name: {
          zh_CN: '页尾',
          en_US: 'Page footer',
        },
      });
    }
    pagesSchema.push(copyPage);
    pagesData.push({
      route: page.route,
      route_handle: '',
      sections: sections.length
        ? sections
        : page.sections.map((el) => {
            return {
              type: el.type,
              fixed: fixedSchemaToData(el.fixed),
              params: '',
            };
          }),
    });
  });
  return { pagesData, pagesSchema };
}

module.exports = { getPagesSetting };
